<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 陈风任 <491085389@qq.com>
 * Date: 2019-3-20
 */

namespace app\user\model;

use think\Db;
use think\Page;
use think\Model;
use think\Config;

/**
 * WEB商城订单维权模型
 */
load_trait('controller/Jump');

class ShopOrderService extends Model
{
    use \traits\controller\Jump;

    //初始化
    protected function initialize()
    {
        // 需要调用`Model`的`initialize`方法
        parent::initialize();
    }

    // 查询售后服务单列表
    public function getOrderGoodsServiceList($post = [])
    {
        $where = [
            'a.status' => ['NOT IN', [8]],
            'a.users_id' => $this->users_id,
        ];
        if (!empty($post['serviceStatus']) && 1 == $post['serviceStatus']) {
            $where['a.status'] = ['IN', [1, 2, 4, 5]];
        } else if (!empty($post['serviceStatus']) && 2 == $post['serviceStatus']) {
            $where['a.status'] = ['IN', [6, 7]];
        }
        $field = 'a.service_id, a.service_type, a.users_id, a.status, a.add_time';
        $field .= ', b.order_code, b.order_status, b.consignee, b.mobile, b.province, b.city, b.district, b.address';
        $field .= ', c.product_name, c.product_price, c.num as product_num, c.litpic as product_img, c.data as product_data';

        // 分页参数
        $Query = request()->param();
        // 查询订单数据
        $result = Db::name('shop_order_service')->alias('a')
            ->field($field)
            ->where($where)
            ->join('__SHOP_ORDER__ b', 'a.order_id = b.order_id', 'LEFT')
            ->join('__SHOP_ORDER_DETAILS__ c', 'a.details_id = c.details_id', 'LEFT')
            ->order('add_time desc, service_id desc')
            ->paginate(15, false, ['query' => $Query]);
        !empty($result) && $serviceData = $result->toArray();
        if (!empty($serviceData['data'])) {
            $orderServiceType = Config::get('global.order_service_type');
            $orderServiceStatus = Config::get('global.order_service_status');
            foreach ($serviceData['data'] as $key => $value) {
                // 添加时间
                $value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
                // 价格处理
                $value['product_price_arr'] = floatval($value['product_price']) * floatval($value['product_num']);
                $value['product_price_arr'] = explode('.', sprintf("%.2f", $value['product_price_arr']));
                // 商品封面图
                $value['product_img'] = handle_subdir_pic(get_default_pic($value['product_img']));
                // 处理商品规格，组合数据
                $product_data = !empty($value['product_data']) ? unserialize($value['product_data']) : [];
                $value['product_attr'] = !empty($product_data['product_attr']) ? $product_data['product_attr'] : '';
                $value['product_spec'] = !empty($product_data['product_spec']) ? $product_data['product_spec'] : '';
                // 状态处理
                $value['status_name'] = $orderServiceStatus[$value['status']];
                $value['service_type_name'] = $orderServiceType[$value['service_type']];
                // 重载数据
                $serviceData['data'][$key] = $value;
            }
        }

        $this->success('查询成功', null, $serviceData);
    }
}